<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
        PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="${daoAllName}">
    <resultMap type="${beanAllName}" id="${beanName}Map">
        <id column="${pri}" property="${pri}"/>
    </resultMap>
<#--${package}.${beanName}${beanPostfix}-->
    <insert id="save${beanName}" parameterType="${beanAllName}">
        insert into ${tableName}(
            <trim prefix="" suffixOverrides=",">
            <#list columns as col>
                <if test="${col.COLUMN_NAME} != null ${col.DATA_TYPE?matches(".*(int|date|decimal).*")?string(""," and ${col.COLUMN_NAME} != ''")}">
                ${col.COLUMN_NAME},
                </if>
            </#list>
            </trim>
        )values(
            <trim prefix="" suffixOverrides=",">
            <#list columns as col>
                <if test="${col.COLUMN_NAME} != null ${col.DATA_TYPE?matches(".*(int|date|decimal).*")?string(""," and ${col.COLUMN_NAME} != ''")}">
                ${r'#{'+col.COLUMN_NAME+'}'},
                </if>
            </#list>
            </trim>
        )
    </insert>
    <insert id="save${beanName}List" parameterType="java.util.ArrayList">
        insert into ${tableName} (
    <#list columns as col>
        <#if col_has_next!=true>
        ${col.COLUMN_NAME}
        <#else>
        ${col.COLUMN_NAME},
        </#if>
    </#list>
        ) VALUES
        <foreach collection="list" item="item" index="index" separator=",">
            (
        <#list columns as col>
            <#if col_has_next!=true>
            ${r'#{item.'+col.COLUMN_NAME+'}'}
            <#else>
            ${r'#{item.'+col.COLUMN_NAME+'}'},
            </#if>
        </#list>
            )
        </foreach>
    </insert>
    <!--  此处long作为通用的有待修改 -->
    <delete id="delete${beanName}ById" parameterType="${pri_type?contains("char")?string("string","long")}">
        delete from ${tableName} where ${pri}=${r'#{'+pri+'}'}
    </delete>
    <update id="update${beanName}ById" parameterType="${beanAllName}">
        update ${tableName}
        <set>
        <#list columns as col>
            <#if !(col.COLUMN_NAME?lower_case)?contains(pri)>
                <if test="${col.COLUMN_NAME} != null ${col.DATA_TYPE?matches(".*(int|date|decimal).*")?string(""," and ${col.COLUMN_NAME} != ''")}">
                ${col.COLUMN_NAME}=${r'#{'+col.COLUMN_NAME+'}'},
                </if>
            </#if>
        </#list>
        </set>
        where ${pri}=${r'#{'+pri+'}'}
    </update>
    <sql id="queryCondition">
    <#list columns as col>
        <#if col.DATA_TYPE?contains("int")>
        <#--整数直接等于-->
            <if test="${col.COLUMN_NAME} != null">
                and ${col.COLUMN_NAME}=${r'#{'+col.COLUMN_NAME+'}'}
            </if>
        <#elseif col.DATA_TYPE?contains("decimal")><#t>
        <#--字符串直接like-->
            <if test="${col.COLUMN_NAME} != null">
                and ${col.COLUMN_NAME}=${r'#{'+col.COLUMN_NAME+'}'}
            </if>
        <#elseif col.DATA_TYPE?contains("char")><#t>
        <#--字符串直接like-->
            <if test="${col.COLUMN_NAME} != null and ${col.COLUMN_NAME} != ''">
                and ${col.COLUMN_NAME} like ${r'"%${'+col.COLUMN_NAME+'}%"'}
            </if>
        <#elseif col.DATA_TYPE?contains("date")>
        <#--日期类型-->
            <if test="${col.COLUMN_NAME}Start != null and ${col.COLUMN_NAME}End != null">
                <![CDATA[
                and ${col.COLUMN_NAME} >= ${r'#{'+col.COLUMN_NAME+'Start}'}
                and ${col.COLUMN_NAME} <= ${r'#{'+col.COLUMN_NAME+'End}'}
                ]]>
            </if>
        <#else >
            <!--TODO 有一个未知类型 ${col.DATA_TYPE} -->
        </#if>
    </#list>
    </sql>
    <select id="get${beanName}ById" parameterType="${pri_type?contains("char")?string("string","long")}" resultMap="${beanName}Map">
        select * from ${tableName}
        where ${pri}=${r'#{'+pri+'}'}
    </select>
    <select id="get${beanName}List" parameterType="${beanAllName}" resultMap="${beanName}Map">
        select * from ${tableName}
        where 1=1
        <include refid="queryCondition"/>
        limit ${r'#{start}'}, ${r'#{size}'}
    </select>
    <select id="count${beanName}List" parameterType="${beanAllName}" resultType="long">
        select count(1) from ${tableName}
        where 1=1
        <include refid="queryCondition"/>
    </select>
</mapper>